﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView
{
    public partial class BianZhi_AnPaiRenYuanWindow : ChildWindow
    {
        public string OrgIDs = "";
        private string Type;
        private string JieYongGuoDu;
        private int OrgID;
        private int KeShiName;
        private int RenShu;
        private JinShengService.JinShengServiceClient _Client = new JinShengService.JinShengServiceClient();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<JinShengService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>();
        private ObservableCollection<EmpbasicService.Emp_BasicInfoModel> Models =
            new ObservableCollection<EmpbasicService.Emp_BasicInfoModel>();
        public BianZhi_AnPaiRenYuanWindow(int orgid, string type, int keshiid, int renshu, string jieyongguodu, ObservableCollection<EmpbasicService.Emp_BasicInfoModel> models)
        {
            InitializeComponent();
            Models = models;
            Type = type;
            OrgID = orgid;
            KeShiName = keshiid;
            JieYongGuoDu = jieyongguodu;
            RenShu = renshu;
            _Client.GetEmpBasicInfoAccordingToDutyCompleted +=
                new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDutyCompleted);
            tbCondition.AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
            Client.UpdateBianZhiAnPaiRenYuanCompleted += 
                new EventHandler<EmpbasicService.UpdateBianZhiAnPaiRenYuanCompletedEventArgs>(Client_UpdateBianZhiAnPaiRenYuanCompleted);
            Load();
        }
        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnAdminSearch_Click(null, null);
            }
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.SelectAll();
            }
        }

        private void tbCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.FontStyle = FontStyles.Normal;
                //tbCondition.Text = e.Key.ToString();
                tbCondition.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SelectItems = null;
            var source = lb.ItemsSource as System.Collections.ObjectModel.ObservableCollection<CommunitySystem.JinShengService.Emp_BasicInfoModel>;
            SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>(source.Where(c => c.IsSelected == true));
            if (SelectItems.Count > 0)
            {
                if(SelectItems.Count > RenShu )
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("没有足够的职位安排人员！只可以安排" + RenShu.ToString() + "个人员！");
                    return;
                }
                if (MessageBox.Show("确定为该人员安排职位吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ObservableCollection<int> li = new ObservableCollection<int>();
                    foreach(JinShengService.Emp_BasicInfoModel i in SelectItems)
                    {
                        li.Add(i.ID);
                    }
                    Client.UpdateBianZhiAnPaiRenYuanAsync(li, KeShiName, JieYongGuoDu, Type);
                    
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要安排职位的人员！");

        }
        void Client_UpdateBianZhiAnPaiRenYuanCompleted(object sender, EmpbasicService.UpdateBianZhiAnPaiRenYuanCompletedEventArgs e)
        {
            if(e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("安置成功！");
                this.DialogResult = true;
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员中有人已被安置，请重新选择！");
            }
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        public void Load()
        {
            string nameOrCard = "1=1";
            if (!string.IsNullOrEmpty(tbCondition.Text) && !tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                nameOrCard = tbCondition.Text;
                if (Type == "正科")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级正职（主任科员）' and DutyAttribute = '领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职' and 
DutyName = (select jigoumingcheng from bianzhi where id = {1}) and (emp.Name like '%{2}%' or emp.CardNo like '%{2}%')", OrgID, KeShiName, nameOrCard));
                if (Type == "副科")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级副职（副主任科员）' and DutyAttribute = '领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职' and 
--DutyName = (select jigoumingcheng from bianzhi where id = {1}) and 
(emp.Name like '%{2}%' or emp.CardNo like '%{2}%')", OrgID, KeShiName, nameOrCard));
                if (Type == "主任")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级正职（主任科员）' and DutyAttribute = '非领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职' and 
--DutyName = (select jigoumingcheng from bianzhi where id = {1}) and 
(emp.Name like '%{2}%' or emp.CardNo like '%{2}%')", OrgID, KeShiName, nameOrCard));
                if (Type == "副主任")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级副职（副主任科员）' and DutyAttribute = '非领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职' and 
DutyName = (select jigoumingcheng from bianzhi where id = {1}) and (emp.Name like '%{2}%' or emp.CardNo like '%{2}%')", OrgID, KeShiName, nameOrCard));

            }
            else
            {
                if (Type == "正科")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级正职（主任科员）' and DutyAttribute = '领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职' and 
DutyName = (select jigoumingcheng from bianzhi where id = {1}) ", OrgID, KeShiName));
                if (Type == "副科")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级副职（副主任科员）' and DutyAttribute = '领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职' and 
DutyName = (select jigoumingcheng from bianzhi where id = {1}) ", OrgID, KeShiName));
                if (Type == "主任")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级正职（主任科员）' and DutyAttribute = '非领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职' ", OrgID));
                if (Type == "副主任")
                    _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID = {0}  and KeChuJi = 0
and DutyLevel = '乡科级副职（副主任科员）' and DutyAttribute = '非领导职务' and JobStatus = '在任' and emp.EmpStatus = '在职'", OrgID));
            }
        }
        private void btnAdminSearch_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        void _Client_GetEmpBasicInfoAccordingToDutyCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
            if(Models != null && Models.Count > 0)
            {
                foreach(JinShengService.Emp_BasicInfoModel i in e.Result)
                {
                    if (Models.Where(p => p.ID == i.ID).FirstOrDefault() != null)
                        i.IsSelected = true;
                }
            }
        }
    }
}
